home *** CD-ROM | disk | FTP | other *** search
- unit ALNew;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls, Grids;
-
- type
- TNewAliasForm = class(TForm)
- Alias: TEdit;
- Label1: TLabel;
- Driver: TComboBox;
- Label2: TLabel;
- OkBtn: TButton;
- CancelBtn: TButton;
- DriverParamsGrid: TStringGrid;
- procedure FormCreate(Sender: TObject);
- procedure OkBtnClick(Sender: TObject);
- procedure DriverClick(Sender: TObject);
- public
- procedure SaveAlias;
- end;
-
- var
- NewAliasForm: TNewAliasForm;
-
- implementation
-
- {$R *.DFM}
-
- uses
- DB, DbiProcs, DbiErrs;
-
- procedure Split( Source: String; SplitChar: Char; var Left, Right: String);
- { Returns in <Left> the substring of <Source> that is to the left of
- the first occurrence of <SplitChar> within <Source>. Returns in
- <Right> the substring of <Source> that is to the right of the first
- occurrence of <SplitChar>. }
- var
- I: Byte;
- begin
- I := 1;
- while (I <= Length(Source)) and (Source[I] <> SplitChar) Do Inc(I);
- Left := Copy(Source, 1, I - 1);
- Right := Copy(Source, I + 1, 255);
- end;
-
- procedure TNewAliasForm.FormCreate(Sender: TObject);
- begin
-
- { Initialize the combo box dropdown list with available BDE drivers }
- Session.GetDriverNames(Driver.Items);
-
- { Initialize the parameters grid }
- with DriverParamsGrid do
- begin
- Cells[0, 0] := 'Parameter:';
- Cells[1, 0] := 'Value:';
- RowCount :=2;
- end;
- end;
-
- procedure TNewAliasForm.DriverClick(Sender: TObject);
- var
- DriverParamsList: TStringList;
- I: Integer;
- ParamName: String;
- ParamValue: String;
- begin
- DriverParamsList := TStringList.Create;
- try
- with Driver do
- Session.GetDriverParams(Items[ItemIndex], DriverParamsList);
-
- { Populate the driver parameters grid }
- with DriverParamsGrid do
- begin
- RowCount := 1;
- for I := 0 to DriverParamsList.Count - 1 do
- begin
- Split(DriverParamsList.Strings[I], '=', ParamName, ParamValue);
- if ParamName <> 'PASSWORD' then
- begin
- Cells[0, I + 1] := ParamName;
- Cells[1, I + 1] := ParamValue;
- RowCount := RowCount + 1;
- end;
- end;
- FixedRows := 1;
-
- SetFocus;
- end;
- finally
- DriverParamsList.Free;
- end;
- end;
-
- procedure TNewAliasForm.OkBtnClick(Sender: TObject);
- begin
- try
- if Alias.Text = '' then
- raise Exception.Create('Must supply an alias name');
- if Driver.ItemIndex = -1 then
- raise Exception.Create('Must supply a driver type');
- SaveAlias;
- except
- ModalResult := mrNone;
- raise;
- end;
- end;
-
- procedure TNewAliasForm.SaveAlias;
- var
- AliasName: array[0..25] of char;
- DriverName: array[0..25] of char;
- Params: PChar;
- TempPasStr: String;
- TempStr: array[0..255] of char;
- I: Integer;
- begin
- StrPCopy(AliasName, Alias.Text);
- StrPCopy(DriverName, Driver.Text);
-
- Params := StrAlloc((DriverParamsGrid.RowCount - 1) * 255);
- try
-
- { Assemble the driver parameters in a single null-terminated string }
- StrPCopy(Params, '');
- with DriverParamsGrid do
- begin
- for I := 0 to RowCount - 2 do
- begin
- TempPasStr := Cells[0, I + 1] + ': ' + Cells[1, I + 1];
- if StrLen(Params) > 0 then TempPasStr := ';' + TempPasStr;
- StrPCopy(TempStr, TempPasStr);
- StrCat(Params, TempStr);
- end;
- end;
-
- case DbiAddAlias(nil, @AliasName, @DriverName, Params, True) of
- DBIERR_INVALIDPARAM: raise Exception.Create('Invalid Param');
- DBIERR_NAMENOTUNIQUE: raise Exception.Create('Alias name already exists');
- DBIERR_OBJNOTFOUND: raise Exception.Create('Invalid driver parameter');
- DBIERR_UNKNOWNDRIVER: raise Exception.Create('Invalid driver');
- end;
- finally
- StrDispose(Params);
- end;
- end;
-
- end.
-